<template>
	<view v-if="showPopup" class="uni-popup" @touchmove.stop.prevent="">
		<view :class="[type, ani, animation ? 'ani' : '']" class="uni-custom uni-popup__wrapper" @click="close(true)">
			<view class="uni-popup__wrapper-box">
				<view class="title">{{authList[permissionID].title}}</view>
				<view class="content">{{authList[permissionID].content}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'AuthCamera',
		props: {
			// 开启动画
			animation: {
				type: Boolean,
				default: true
			},
			//弹出层类型，可选值，top: 顶部弹出层；bottom：底部弹出层；center：全屏弹出层,
			type: {
				type: String,
				default: 'center'
			},
			show: {
				type: Boolean,
				default: true
			},
			permissionID: {
				type: [String, Number],
				default: ''
			}
		},
		data() {
			return {
				ani: '',
				showPopup: false,
				StatusBar: '',
				authList: {
					'CAMERA': {
						title: "相机权限申请说明",
						content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像、保存相册、项目上传/分享、下载等场景中读取和写入相册和文件内容。"
					},
					'READ_EXTERNAL_STORAGE': {
						title: "相册权限申请说明",
						content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像、保存相册、项目上传/分享、下载等场景中读取和写入相册和文件内容。",
					},
					'ACCESS_FINE_LOCATION': {
						title: "地理位置权限申请说明",
						content: "便于应用程序可以提供基于位置的服务、定位导航、附近搜索等功能。"
					},
					'CALL_PHONE': {
						title: "拨打/管理电话权限申请说明",
						content: "便于您使用该功能联系平台或者客服、业务经理与联系等场景下使用"
					}
				}
			}
		},
		methods: {
			open(type) {
				this.requestPermissions(this.permissionID);
			},
			close(type) {
				this.ani = ''
				this.$nextTick(() => {
					setTimeout(() => {
						this.showPopup = false
					}, 300)
				})
			},
			//权限检测
			requestPermissions(permissionID) {
				let _this = this;
				let _permissionID;
				_permissionID = 'android.permission.' + permissionID;
				// #ifdef APP-PLUS
				//判断安卓与ios设备
				if (plus.os.name == 'Android') {
					plus.android.checkPermission(_permissionID,

						granted => {
							console.log('获取是否授权相机了---------', granted, this.permissionID)
							if (granted.checkResult == -1) {
								//还未授权当前查询的权限，打开权限申请目的自定义弹框
								_this.showPopup = true;
								_this.$nextTick(() => {
									setTimeout(() => {
										_this.ani = 'uni-' + _this.type
									}, 30)
								})
							}
						},
						error => {
							console.log(error.message);
						}
					);
					plus.android.requestPermissions([_permissionID],
						(e) => {
							console.log('获取下面的是否授权相机了', e)
							//关闭权限申请目的自定义弹框
							_this.ani = '';
							_this.$nextTick(() => {
								setTimeout(() => {
									_this.showPopup = false
								}, 300)
							})
							if (e.granted.length > 0) {
								//当前查询权限已授权，此时可以通知页面执行接下来的操作
								_this.$emit('changeAuthCamera');
							} else {
								_this.$emit('changeAuthClose', e.granted.length);
							}
						})
				} else {
					//IOS不需要，在配置文件的隐私信息访问的许可描述里可添加
				}
				// #endif
			}
		}
	}
</script>
<style lang="scss">
	.uni-popup {
		position: fixed;
		/*  #ifdef  H5  */
		top: 0px;
		// top: 50px;
		/*  #endif  */
		/*  #ifndef  H5  */
		top: 0px;
		/*  #endif  */
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 99999;
		overflow: hidden;
		background-color: rgba(0, 0, 0, 0.8);

		&__wrapper {
			position: absolute;
			z-index: 999;
			box-sizing: border-box;

			&.ani {
				transition: all 0.3s;
			}

			&.top {
				top: 0;
				left: 0;
				width: 100%;
				transform: translateY(-100%);
			}

			&.bottom {
				bottom: 0;
				left: 0;
				width: 100%;
				transform: translateY(100%);
			}

			&.right {
				bottom: 0;
				left: 0;
				width: 70%;
				transform: translateY(100%);
			}

			&.center {
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				transform: scale(1.2);
				opacity: 0;
			}

			&-box {
				position: relative;
				box-sizing: border-box;
			}

			&.uni-custom {
				& .uni-popup__wrapper-box {
					padding: 10vh 3vh 100vh;
					color: white;

					.title {
						font-size: 36rpx;
						font-weight: bold;
					}

					.content {
						margin-top: 18rpx;
						line-height: 1.6;
					}
				}

				&.center {
					& .uni-popup__wrapper-box {
						position: relative;
						max-width: 80%;
						max-height: 80%;
						overflow-y: scroll;
					}
				}

				&.top,
				&.bottom {
					& .uni-popup__wrapper-box {
						width: 100%;
						max-height: 1000upx;
						// overflow-y: scroll;
					}
				}
			}

			&.uni-top,
			&.uni-bottom {
				transform: translateY(0);
			}

			&.uni-center {
				transform: scale(1);
				opacity: 1;
			}
		}
	}
</style>